package com.youlu.campus.admin.cms.controller;

import com.youlu.campus.admin.cms.entity.ComplaintInfo;
import com.youlu.campus.admin.cms.service.ComplaintInfoService;
import com.youlu.campus.admin.cms.vo.ComplaintInfoQueryRequest;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;

/**
 * Created by zhuhuaiqi 投诉管理
 */
@Slf4j
@RestController
public class ComplaintInfoController {
    @Autowired
    private ComplaintInfoService complaintInfoService;

    @PostMapping("/complaint/list")
    public RestResult list(@RequestBody @Valid ComplaintInfoQueryRequest req) {
        String comId = AuthUtils.getComId();
        req.setTenantId(comId);
        Page<ComplaintInfo> datas = complaintInfoService.query(req);
        if (datas == null) {
            PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getPageSize());
            datas = PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> 0L);
        }
        return RestResultBuilder.builder().success().data(datas).build();
    }

    @PostMapping("/complaint/black-list/{userId}")
    public RestResult setBlackList(@PathVariable(name = "userId") String userId,
                                   @RequestParam(name = "status") String status,
                                   @RequestParam(name = "id") String id) {
        String comId = AuthUtils.getComId();
        boolean r = complaintInfoService.setBlackList(id, userId, status);
        log.info(":>>> 设置用户黑名单结果为:{}", r);
        if (r) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    /**
     * 状态,1:隐藏,0:显示
     * @param contentId
     * @param type
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/complaint/update/content-status/{contentId}")
    public RestResult updateContentStatus(@PathVariable(name = "contentId") String contentId,
                                          @RequestParam Integer type,
                                          @RequestParam Integer status,
                                          @RequestParam String id) {
        String comId = AuthUtils.getComId();
        boolean r = complaintInfoService.updateContentStatus(id,contentId, type, status);
        log.info(":>>> 修改内容状态结果为:{}", r);
        if (r) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }
}
